#!/bin/bash

CURRENT_SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)

(cd "$CURRENT_SCRIPT_DIR" && gcc main.c -o keyboard-shortcut)
 
if [[ ! -f "$CURRENT_SCRIPT_DIR/keyboard-shortcut" ]];then
    echo "Error: $CURRENT_SCRIPT_DIR/keyboard-shortcut not found!"
    exit 1
fi

service_file="$CURRENT_SCRIPT_DIR/keyboard-shortcut-for-utools.service"

cat <<EOF | tee "$service_file"
[Unit]
Description=Keyboard shortcut for utools (run utools while LeftAlt double clicked)

[Service]
Type=simple
ExecStart="$CURRENT_SCRIPT_DIR/keyboard-shortcut"

[Install]
WantedBy=multi-user.target
EOF

sudo cp -f "$service_file" /etc/systemd/system/

sudo systemctl daemon-reload

# 启动服务	 
sudo systemctl start keyboard-shortcut-for-utools.service

# 设置服务在系统启动时自动启动	 
sudo systemctl enable keyboard-shortcut-for-utools.service

echo "服务设置完成。你可以使用 sudo systemctl status keyboard-shortcut-for-utools.service 命令查看服务的执行情况"
echo "还可以使用 journalctl -u  keyboard-shortcut-for-utools.service 命令查看服务的日志"